import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:Dao数据访问对象,访问数据库的操作
 * User: lenovo
 * Date: 2022-12-05
 * Time: 10:52
 */
public class BlogDao {

//    插入博客 传入一个博客对象
    public void  insert(Blog blog) {
        Connection connection = null;
        PreparedStatement statement = null;

        //1.建立连接
        try {
             connection = DbUtil.getConnection();
            //2.构造sql语句
            String sql = "insert into blog values(null,?,now(),?,?);";
            statement = connection.prepareStatement(sql);
            statement.setString(1,blog.getTitle());
            statement.setString(2, blog.getContent());
            statement.setInt(3,blog.getUserId());
            //3.执行sql
            int ret = statement.executeUpdate();
            if(ret == 1){
                System.out.println("插入成功!");
            }else {
                System.out.println("插入失败!");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //4.关闭资源
            DbUtil.close(null,statement,connection);
        }
    }
//    删除博客 根据博客id来删除博客
    public void delete(int blogId)  {
        Connection connection = null;
        PreparedStatement statement = null;

        //1.建立连接
        try {
            connection = DbUtil.getConnection();
            //2.构造sql语句
            String sql = "delete from blog where blogId = ?;";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            //3.执行sql
            int ret = statement.executeUpdate();
            if(ret == 1){
                System.out.println("插入成功!");
            }else {
                System.out.println("插入失败!");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //4.关闭资源
            DbUtil.close(null,statement,connection);
        }


    }
//    博客列表 查表操作
    public List<Blog> selectAll()  {
        //创建一个链表用来装blog对象
        List<Blog> list = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        //1.建立连接
        try {
            connection = DbUtil.getConnection();
            //2.构造sql语句
            String sql = "select * from blog order by postTime desc;";//时间降序排序,最新发布的博客在最上面
            statement = connection.prepareStatement(sql);
            //3.执行sql
            resultSet = statement.executeQuery();
            //4.遍历集合:将数据库里的记录设置到java的blog对象里
            while (resultSet.next()) {
                //先创建一个blog对象:一篇博客
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                //给blog的content内容做出限制
                 String content = resultSet.getString("content");
                 if(content.length()>100){
                    content = content.substring(0,100)+"...";
                 }
                blog.setContent(content);
                blog.setUserId(resultSet.getInt("userId"));
                //将设置好的blog对象添加到List<Blog>里面
                list.add(blog);
            }


        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //4.关闭资源
            DbUtil.close(resultSet, statement, connection);
        }
        return list;
    }


//    博客详情页 根据博客id来查询指定blog的详情
    public Blog selectOne(int blogId){
            Connection connection = null;
            PreparedStatement statement = null;
            ResultSet resultSet = null;
            Blog blog = null;

            //1.建立连接
            try {
                connection = DbUtil.getConnection();
                //2.构造sql语句
                String sql = "select * from blog where blogId = ?;";
                statement = connection.prepareStatement(sql);
                statement.setInt(1, blogId);
                //3.执行sql
                resultSet = statement.executeQuery();
                while (resultSet.next()){
                    //创建一个blogjava对象
                    blog = new Blog();
                    blog.setBlogId(resultSet.getInt("blogId"));
                    blog.setTitle(resultSet.getString("title"));
                    blog.setPostTime(resultSet.getTimestamp("postTime"));
                    blog.setContent(resultSet.getString("content"));
                    blog.setUserId(resultSet.getInt("userId"));

                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                //4.关闭资源
                DbUtil.close(resultSet,statement,connection);
            }
            return blog;


        }

    }


